package com.bosssoft.service.oss.controller;

import com.bosssoft.base.entity.Result;
import com.bosssoft.service.oss.service.IOssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.RequestScope;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * oss控制器
 *
 * @author wzy
 * @author Echo
 * @since 2021-04-10 20:08
 */
@Slf4j
@RestController
@RequestMapping("/api")
@RequestScope
@Api(tags = "1-文件上传api接口")
public class OssController {

    @Resource
    private IOssService ossService;

    /**
     * 带模块的文件上传
     *
     * @param file   文件
     * @param module 模块名称
     * @return 地址
     */
    @ApiOperation(value = "上传文件，带模块名")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件对象",dataType = "file",paramType = "form"),
            @ApiImplicitParam(name = "module", value = "模块名", required = true,dataType = "String")
    })
    @PostMapping("/moduleUpload")
    public Result<String> upload(@RequestPart MultipartFile file, @RequestParam("module") String module) {
        //返回oss路径
        String url = ossService.upload(file, module);
        log.info("oss地址" + url);
        return Result.ok(url);
    }


    /**
     * 文件上传，模块默认为other
     *
     * @param file 文件
     * @return 地址
     */
    @ApiOperation(value = "上传文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件对象",dataType = "file",paramType = "form")
    })
    @PostMapping("/upload")
    public Result<String> upload(@RequestPart MultipartFile file) {
        //返回oss路径
        String url = ossService.upload(file);
        log.info("oss地址" + url);
        return Result.ok(url);
    }


    /**
     * 带模块文件流下载
     *
     * @param module   OSS上的路径，不包含桶名称，路径末尾添加
     * @param fileName 文件名称，包含后缀
     * @return 输入流
     * @version 0.1.0
     * @author Echo
     * @since 2021/4/15 10:45
     */
    @ApiOperation(value = "下载文件，带模块名")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "module", value = "模块名", required = true,dataType = "String"),
            @ApiImplicitParam(name = "fileName", value = "文件名",required = true,dataType = "String")
    })
    @GetMapping("/moduleDownload")
    public Result<byte[]> download(@RequestParam("module") String module, @RequestParam("fileName") String fileName) {
        byte[] bytes = ossService.download(module, fileName);
        return Result.ok(bytes);
    }

    /**
     * 默认文件流下载，默认模块名为other
     *
     * @param fileName 文件名称，包含后缀
     * @return 携带流的结果
     * @version 0.1.0
     * @author Echo
     * @since 2021/4/15 10:45
     */
    @ApiOperation(value = "下载文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileName", value = "文件名",required = true,dataType = "String")
    })
    @GetMapping("/download")
    public Result<byte[]> download(@RequestParam("fileName") String fileName) {
        byte[] bytes = ossService.download(fileName);
        return Result.ok(bytes);
    }

}
